<template>
  <div class="choice-commidy">
    <div class="already-content">
      <EditTable
        ref="editTable"
        @onChangeSelectAll="onSelectAll"
        @onJoinSale="onJoinSale"
        @onJoinSaleCheck="onJoinSaleCheck"
        @onShowChild="onShowChild"
        @onSetgiftPoint="onSetgiftPoint"
        @onSetGiftRebate="onSetGiftRebate"
        @onSetsellOutQuantity="onSetsellOutQuantity"
        @onSetWarningQuantity="onSetWarningQuantity"
        @onSetgiftCouponId="onSetgiftCouponId"
        @onSetSale="onSetSale"
        :listData="columns"
        :list="$store.state.bargain.bargainData"
      ></EditTable>
      <div class="pager">
        <Page 
          :total="totalItem" 
          :current="currentPage"
          :page-size="pageSize"
          @on-change="onPageChange"
          @on-page-size-change="onPageSizeChange"
          show-sizer 
          placement="top">
        </Page>
      </div>
    </div>
    <Loader :status="status"></Loader>
  </div>
</template>
<script>
import EditTable from './ChoiceTable'
import {columns} from './ChoiceCommidyData'
import {setArrayValue, getMinValue, getMaxValue, isPriceEmpty} from "@/views/sales/common/TableSaleMethods";
export default {
  components: {
    EditTable
  },
  data() {
    return {
      columns,
      status: 'pass',
      id: this.$route.query.id || '',
      type: this.$route.query.type || '',
      totalItem: null,
      pageSize: window.$pageSize,
      currentPage: 1
    }
  },
  mounted() {
    this.getProductData({size: window.$pageSize, current: 1, self: this})
  },
  methods: {
    onPageChange(val) {
      this.status = 'loading'
      this.currentPage = Number(val)
      this.$store.dispatch('getPrucductData', {size: this.pageSize, current: this.currentPage, self: this}).then(res => {
        this.$refs.editTable.selectAll = false
      })
    },
    onPageSizeChange(val) {
      this.status = 'loading'
      this.pageSize = val
      this.$store.dispatch('getPrucductData', {size: this.pageSize, current: this.currentPage, self: this}).then(res => {
        this.$refs.editTable.selectAll = false
      })
    },
    getProductData(param) {
      param.self = this
      this.status = 'loading'
      this.$store.dispatch('getPrucductData', param).then(data => {
        this.$refs.editTable.selectAll = false
        this.$refs.editTable.totalItem =  data.total
      })
    },
    getSaleData(type) {
      let text = '限制品牌'
      if (type == 2) text = "不限制品牌"
      if(isPriceEmpty(this.$store.state.bargain.bargainData, {index: "sellOutQuantity", index1: "checked",childName: "productSkuList"})) {
        this.$Message.error(`${text}促销库存不能为空`)
        return false
      }
      if(isPriceEmpty(this.$store.state.bargain.bargainData, {index: "sale", index1: "checked",childName: "productSkuList"})) {
        this.$Message.error(`${text}促销价不能为空`)
        return false
      }
      this.$store.commit('bargainGetSale', {id: this.id, type: type, self: this})
      if (this.id && this.type) {
        // this.$emit('complete', 'UNDER')
      }
    },
    onJoinSaleCheck(item, todo, selectAll) {
      // 点击checked按钮的时候触发的事件
      this.onJoinSaleBody(item, todo, {
        index: "checked",
        index1: "productSkuList"
      });
    },
    onSelectAll(param) {
      //点击选中所有checked按钮的时候出发的事件
      param ? setArrayValue(this.$store.state.bargain.bargainData, {
        index: "checked",
        val1: true,
        childName: "productSkuList"
      }) : setArrayValue(this.$store.state.bargain.bargainData, {
        index: "checked",
        val1: false,
        childName: "productSkuList"
      })
    },
    onJoinSaleBody(item, todo) {
      if (item.productSkuList && item.productSkuList.length > 0) {
        item.checked ? setArrayValue(item.productSkuList, {
          index: "checked",
          val1: true,
          childName: "productSkuList"
        }) : setArrayValue(item.productSkuList, {
          index: "checked",
          val1: false,
          childName: "productSkuList"
        });
      } else if (!item.productSkuList) {
        /* 当是子集的时候，选择一个对应的父级就选上 */
        let status = false;
        if (!todo) {
          return false;
        }
        for (let child of todo.productSkuList) {
          if (child.checked) {
            status = true;
          }
        }
        if (!status) {
          //子集没有被选中
          todo.checked = false;
        } else {
          todo.checked = true;
        }
      }
    },
    onShowChild(item) {
      item.showChildren = !item.showChildren
    },
    onSetgiftPoint(item, todo) {
      this.onSet(item, todo, 'giftPoint')
    },
    onSetGiftRebate(item, todo) {
      this.onSet(item, todo, 'giftRebate')
    },
    onSetsellOutQuantity(item, todo) {
      this.onSet(item, todo, 'sellOutQuantity')
    },
    onSetWarningQuantity(item, todo) {
      this.onSet(item, todo, 'warningQuantity')
    },
    setChild(data, type) {
      for (let item of data.productSkuList) {
        item[type] = type == 'sellOutQuantity' ? Number(data[type]).toFixed(0) : Number(data[type])
      }
    },
    onSetSale(item, todo) {
      this.onSet(item, todo, 'sale')
    },
    onSet(item, todo, type) {
      if(!item[type] || !/^([\+ \-]?([1-9]\d*)|(0))([.]\d{0,2})?$/.test(item[type])) {
        return false;
      } 
      if (item.productSkuList && item.productSkuList.length > 0) {
        this.setChild(item, type)
      } else if (!item.productSkuList) {
        todo[type] =`${getMinValue(todo.productSkuList, {index: type}) || 0} - ${getMaxValue(todo.productSkuList, {index: type})}`
      }
    },
    onSetgiftCouponId(data) {      
      for (let item of data.productSkuList) {
        item['giftCouponId'] = data['giftCouponId']
      }
    }
  }
}
</script>

